//
//  UserModel.swift
//  DaRings
//
//  Created by 易先生 on 2022/10/29.
//

import UIKit
import HandyJSON
import MYModule


public struct UserModel : HandyJSON {
    
    public enum Sex: Int, CaseIterable, HandyJSONEnum {
        case male = 0
        case female = 1
        
        public var localeName: String {
            
            switch self {
            case .male:
                return "unit_male".language
            case .female:
                return "unit_female".language
            }
        }
    }
    
    /// 单位 0 公制 1英制
    public var unitType : UnitType = .metric {
        
        didSet {
            
            Unit.sharedInstance.myType = unitType
            
        }
        
    }
        
    /// 心率预警最大心率
    public var warningMxHR: Int = 160
    
    /// 久坐提醒是否打开
    public var sitRemind: Bool = false
    
    /// 低电量提醒
    public var lowBatteryReminder: Bool = true
    
    /// 低电量提醒时间间隔(半小时一次)
    public var lowBatteryReminderTime: Int = 30
    
    /// 心率预警提醒
    public var heartRemind: Bool = false
    
    public var weight: Int = 65
    
    public var height: Int = 170
    
    public var birth: Int = 0
    
    public var steplength: Int = 0
    
    /// 1女 0男
    public var sex: Sex = .male
    
    public init() {}
    
    public init(unitType: UnitType, warningMxHR: Int, sitRemind: Bool, lowBatteryReminder: Bool, weight: Int, height: Int, birth: Int, steplength: Int, sex: Sex) {
        
        self.unitType = unitType
        self.warningMxHR = warningMxHR
        self.sitRemind = sitRemind
        self.lowBatteryReminder = lowBatteryReminder
        self.weight = weight
        self.height = height
        self.birth = birth
        self.steplength = steplength
        self.sex = sex
        
    }
    
    
    public var sexs: [String] {
        
        return Sex.allCases.map { $0.localeName }
    }
    
    /// 年龄
    public var age: Int {
        
        var currentAge = 0
        let currentTimestamp = Date().timeIntervalSince1970
        if Int(currentTimestamp) >= birth {
            currentAge = currentAge + 1
        }
        let birthYear = Date(timeIntervalSince1970: TimeInterval(birth))
        return Date().year + currentAge - birthYear.year
    }
    
    public func getMaxHR() -> String {
        guard warningMxHR>0 else {
            return "160BPM"
        }
        return "\(warningMxHR)BPM"
        
    }
    
    public var defaultMaxHR: Int {
        get{
            return Int(220 - age)
        }
    }
}

extension UserModel {
    
    /**
     * 体质指数 (BMI) = 体重 （kg） / 身高二次方（m）
     *
     * @param height 身高
     * @param weight 体重 kg
     */
    
    public func BMI(height: Float, weight: Float) ->Float {
        
        if height <= 0 || weight <= 0 {
            return 0
        }
        
        return weight/(height * height)
    }
    
    /**
     
     * 根据BMI获得目标步数
     *
     * @param bmi 体质指数
     
     * @return 每日目标步数
     
     */

    public func getGoalSteps(bmi: Float) ->Int {

            var steps = 0

            if (bmi < 18.5) {           // 8000

                steps = 8000

            } else if (bmi < 23.9) {    // 9000

                steps = 9000

            } else if (bmi < 26.9) {    // 10000

                steps = 10000

            } else if (bmi < 29.9) {    // 11000

                steps = 11000

            } else {                    // 12000

                steps = 12000

            }

            return steps

        }


        /**

         * 根据BMI获得目标卡路里

         */

    public func getGoalCalories(bmi: Float) ->Int {

            var calories = 0;

            if (bmi < 18.5) {           //步数 8000

                calories = 300;

            } else if (bmi < 23.9) {    //步数 9000

                calories = 400;

            } else if (bmi < 26.9) {    // 步数10000

                calories = 500;

            } else if (bmi < 29.9) {    //步数 11000

                calories = 600;

            } else {                    //步数 12000

                calories = 700;

            }

            return calories;

        }



        /**

         * 根据BMI获得目标锻炼时长

         */

    public func getGoalExerciseTime(bmi: Float) ->Int {

            var time = 0

            if (bmi < 18.5) {           // 步数 8000

                time = 30

            } else if (bmi < 23.9) {    // 步数 9000

                time = 40

            } else if (bmi < 26.9) {    // 步数 10000

                time = 50

            } else {                    // 步数 12000

                time = 60

            }

            return time

        }
    
    
}

extension UserModel {
    /**
     * 根据身高获取步长
     *
     * @param height 身高（单位：cm）
     * @return
     */
    public func getStepLength(height: Int) -> Int {
        var stepLength = 83
        if height >= 175 {
            stepLength = height - 8 >> 1
        } else if height > 156 {
            stepLength = height - 12 >> 1
        } else {
            stepLength = height - 16 >> 1
        }
        stepLength = lround(Double(stepLength) * 0.85)
        if Unit.sharedInstance.myType == .imperial{
            stepLength = lround(Double(stepLength) * 0.3937)
        }
        if Unit.sharedInstance.myType == .metric{
            if stepLength < 30 {
                stepLength = 30
            }
            if stepLength > 155 {
               stepLength = 155
            }
        }else {
            if stepLength < 12 {
                stepLength = 12
            }
            if stepLength > 60 {
                stepLength = 60
            }
        }
        return stepLength
    }
}

extension UserModel {

    /// 睡眠设置
    /// - Returns: 小时数
   public func getSleepGoal()->Int {
       var goal = 8
       
       if (age <= 2) {
           goal = 12
       } else if (age <= 5) {
           goal = 11
       } else if (age <= 13) {
           goal = 10
       } else if (age <= 17) {
           goal = 9
       } else if (age <= 64) {
           goal = 8
       } else {
           goal = 7
       }
       
       return goal
   }
    
}


public extension UnitType {
    
    var localeName: String {
        
        switch self {
        case .metric:
            return "unit_unit_system_metric".language
            
        case .imperial:
            return "unit_unit_system_english".language
            
        }
        
    }
    
}

